package com.xoa.plugin.workflow.workflowservice.imlp.lnsfplugin;

import com.xoa.dev.efficiency.model.FlowAbilityMonitor;
import com.xoa.model.users.Users;
import com.xoa.plugin.workflow.workflowservice.WorkFlowService;
import com.xoa.util.ToJson;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Major_6 implements WorkFlowService {
    @Override
    public ToJson run(HttpServletRequest request) {
        LnsfUtil lnsfUtil=LnsfUtil.init();
        String runId=request.getParameter("runId");
        String flowId=request.getParameter("flowId");
        Users users=lnsfUtil.getUser(request);
        String fuzexiaolingdao=lnsfUtil.getWorkNameValue(flowId,runId,"负责校领导");
        Map<String,String> lingdao=lnsfUtil.getIds(fuzexiaolingdao);
        //如果登陆的是校领导
        if(users.getUserId().equals(lingdao.get("USER_ID"))){
            FlowAbilityMonitor flowAbilityMonitor=new FlowAbilityMonitor();
            flowAbilityMonitor.setUserId(lingdao.get("USER_ID"));
            flowAbilityMonitor.setRunId(Integer.parseInt(runId));
            flowAbilityMonitor.setUserType(1);//人员类别负责校领导
            flowAbilityMonitor.setWorkEnd(lnsfUtil.getSdf().format(new Date()));
            lnsfUtil.setData(flowAbilityMonitor);
        }else{
            FlowAbilityMonitor flowAbilityMonitor=new FlowAbilityMonitor();
            flowAbilityMonitor.setUserId(lingdao.get("USER_ID"));
            flowAbilityMonitor.setRunId(Integer.parseInt(runId));
            flowAbilityMonitor.setUserType(6);//人员类别负责校领导
            flowAbilityMonitor.setWorkEnd(lnsfUtil.getSdf().format(new Date()));
            lnsfUtil.setData(flowAbilityMonitor);
        }
        //获取转交的步骤
        String nextWorks=request.getParameter("flowPrcs");
        //如果没有逗号，为了方便添加上逗号
        if (nextWorks != null && !nextWorks.contains(",")) {
            nextWorks = nextWorks + ",";
        }
        //用逗号分割
        String[] nextWorkArray = nextWorks.split(",");
        //获取下一步主办人
        String[] prcsOpUser=lnsfUtil.getPrcsOpUser(request.getParameter("beginUser"));
        //获取下一步经办人
        //List<String[]> prcsUserList=getPrcsUser(triggerPlugIn.getPrssUser());
        if(nextWorkArray!=null){
            Map<String,Map<String,String>> nextArr=new HashMap<>();
            for(int i=0;i<nextWorkArray.length;i++){
                String nextWork=nextWorkArray[i];
                String nextOpUser=prcsOpUser[i];
                String userName=lnsfUtil.getUsersMapper().getUsernameByUserId(nextOpUser);
                Map<String,String> nameIdMap=new HashMap<>();
                nameIdMap.put("name",userName);
                nameIdMap.put("userId",nextOpUser);
                nextArr.put(nextWork,nameIdMap);
            }
            if(nextArr.get("9")!=null&&!nextArr.get("9").get("name").equals(fuzexiaolingdao)){//继续本步骤
                String outTime=lnsfUtil.getWorkDeadline(flowId,9);
                Map map=new HashMap();
                map.put("runId",runId);
                map.put("flowPrcs",9);
                map.put("userId",nextArr.get("9").get("userId"));
                Map<String,String> rowMap=lnsfUtil.getFlowAbilityMonitorMapper().selectIdByMap(map);
                if(rowMap!=null){
                    FlowAbilityMonitor flowAbilityMonitor1=new FlowAbilityMonitor();
                    flowAbilityMonitor1.setRunId(Integer.parseInt(runId));
                    flowAbilityMonitor1.setUserId(nextArr.get("9").get("userId"));
                    String deptId=lnsfUtil.getFlowAbilityMonitorMapper().getDeptIdByUserId(nextArr.get("9").get("userId"));
                    flowAbilityMonitor1.setDeptId(deptId);
                    flowAbilityMonitor1.setWorkType(1);
                    flowAbilityMonitor1.setUserType(6);
                    flowAbilityMonitor1.setDeptType(6);
                    flowAbilityMonitor1.setWorkDeadline(outTime);
                    flowAbilityMonitor1.setWorkStart(rowMap.get("PRCS_TIME"));
                    flowAbilityMonitor1.setWorkTitle("学校领导意见");
                    lnsfUtil.setData(flowAbilityMonitor1);
                }
            }else if(nextArr.get("10")!=null){
                String outTime=lnsfUtil.getWorkDeadline(flowId,10);
                Map map=new HashMap();
                map.put("runId",runId);
                map.put("flowPrcs",10);
                map.put("userId",nextArr.get("10").get("userId"));
                Map<String,String> rowMap=lnsfUtil.getFlowAbilityMonitorMapper().selectIdByMap(map);
                if(rowMap!=null){
                    FlowAbilityMonitor flowAbilityMonitor1=new FlowAbilityMonitor();
                    flowAbilityMonitor1.setRunId(Integer.parseInt(runId));
                    flowAbilityMonitor1.setUserId(nextArr.get("10").get("userId"));
                    String deptId=lnsfUtil.getFlowAbilityMonitorMapper().getDeptIdByUserId(nextArr.get("10").get("userId"));
                    flowAbilityMonitor1.setDeptId(deptId);
                    flowAbilityMonitor1.setWorkType(1);
                    flowAbilityMonitor1.setUserType(7);
                    flowAbilityMonitor1.setDeptType(7);
                    flowAbilityMonitor1.setWorkDeadline(outTime);
                    if(rowMap.get("PRCS_TIME")!=null){
                        flowAbilityMonitor1.setWorkStart(rowMap.get("PRCS_TIME"));
                        flowAbilityMonitor1.setWorkTitle("机要科打印文档");
                    }else{
                        flowAbilityMonitor1.setWorkStart(lnsfUtil.getSdf().format(new Date()));
                    }
                    lnsfUtil.setData(flowAbilityMonitor1);
                }
            }
        }
        return null;
    }
}
